﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Demo_MessageBox.View
{
    /// <summary>
    /// WindowError.xaml 的交互逻辑
    /// </summary>
    public partial class WindowError : Window
    {
        public WindowError()
        {
            InitializeComponent();
            WindowStartupLocation = WindowStartupLocation.CenterScreen;//窗体显示在正中间
            BackgroundWorkerInit();
        }

        private BackgroundWorker BackgroundWorker = null;

        private void BackgroundWorkerInit()//需要初始化
        {
            BackgroundWorker = new BackgroundWorker();
            BackgroundWorker.DoWork += BoxClosedClick;
            BackgroundWorker.WorkerSupportsCancellation = true; //允许取消
        }

        private void BoxClosedClick(object sender, DoWorkEventArgs e)
        {
            try
            {
                int nSeconds = 10;
                for (int i = nSeconds; i > 0; i--)
                {
                    System.Windows.Application.Current.Dispatcher.BeginInvoke((Action)delegate ()
                    {
                        TimeClose.Text = string.Format("({0})", i.ToString());
                        SetWindowPos(handle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
                        //WinMainx.Opacity = i == nSeconds ? 1 : double.Parse(string.Format("0.{0}", i));
                    });
                    Thread.Sleep(1000);
                }
                System.Windows.Application.Current.Dispatcher.BeginInvoke((Action)delegate ()
                {
                    WinMainx.Close();
                });
            }
            catch (System.Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }

        }

        #region 窗体置顶

        [DllImport("user32.dll")]
        private static extern int SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, int uFlags);
        private IntPtr handle;
        private const int SWP_NOMOVE = 2;    //不调整窗体位置
        private const int SWP_NOSIZE = 1;    //不调整窗体大小
        private static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);    //窗体置顶
        private static readonly IntPtr HWND_NOTOPMOST = new IntPtr(-2);    //取消窗体置顶

        #endregion

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            #region 获取窗体句柄
            WindowInteropHelper wndHelper = new WindowInteropHelper(this);
            handle = wndHelper.Handle;
            #endregion

            BackgroundWorker.RunWorkerAsync();//开始执行DoWork
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            BackgroundWorker.CancelAsync();//停止执行DoWork
        }

        private void Button_OK_Click(object sender, RoutedEventArgs e)
        {
            WinMainx.Close();
        }
    }
}
